package com.springdoor.web.controller.goods;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springdoor.common.annotation.Log;
import com.springdoor.common.core.controller.BaseController;
import com.springdoor.common.core.domain.AjaxResult;
import com.springdoor.common.enums.BusinessType;
import com.springdoor.common.core.domain.entity.SprdGoodsAttrVal;
import com.springdoor.goods.service.ISprdGoodsAttrValService;
import com.springdoor.common.utils.poi.ExcelUtil;
import com.springdoor.common.core.page.TableDataInfo;

/**
 * 商品属性值Controller
 *
 * @author llshs
 * @date 2024-05-08
 */
@RestController
@RequestMapping("/goods/attrval")
public class SprdGoodsAttrValController extends BaseController
{
    @Autowired
    private ISprdGoodsAttrValService sprdGoodsAttrValService;

    /**
     * 查询商品属性值列表
     */
    @PreAuthorize("@ss.hasPermi('goods:attr:list')")
    @GetMapping("/list")
    public TableDataInfo list(SprdGoodsAttrVal sprdGoodsAttrVal)
    {
        startPage();
        List<SprdGoodsAttrVal> list = sprdGoodsAttrValService.selectSprdGoodsAttrValList(sprdGoodsAttrVal);
        return getDataTable(list);
    }

    /**
     * 导出商品属性值列表
     */
    @PreAuthorize("@ss.hasPermi('goods:attr:export')")
    @Log(title = "商品属性值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SprdGoodsAttrVal sprdGoodsAttrVal)
    {
        List<SprdGoodsAttrVal> list = sprdGoodsAttrValService.selectSprdGoodsAttrValList(sprdGoodsAttrVal);
        ExcelUtil<SprdGoodsAttrVal> util = new ExcelUtil<SprdGoodsAttrVal>(SprdGoodsAttrVal.class);
        util.exportExcel(response, list, "商品属性值数据");
    }

    /**
     * 获取商品属性值详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:attr:query')")
    @GetMapping(value = "/{valueId}")
    public AjaxResult getInfo(@PathVariable("valueId") Long valueId)
    {
        return success(sprdGoodsAttrValService.selectSprdGoodsAttrValByValueId(valueId));
    }

    /**
     * 新增商品属性值
     */
    @PreAuthorize("@ss.hasPermi('goods:attr:add')")
    @Log(title = "商品属性值", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SprdGoodsAttrVal goodsAttrVal)
    {
        if (!sprdGoodsAttrValService.checkGoodsAttrValKeyUnique(goodsAttrVal))
        {
            return error("新增属性值失败，属性键值" + goodsAttrVal.getValueKey() + "已存在");
        }
        if (!sprdGoodsAttrValService.checkGoodsAttrValLabelUnique(goodsAttrVal))
        {
            return error("新增属性值失败，属性值" + goodsAttrVal.getValueLabel() + "已存在");
        }
        goodsAttrVal.setCreateBy(getUsername());
        goodsAttrVal.setUpdateBy(getUsername());
        return toAjax(sprdGoodsAttrValService.insertSprdGoodsAttrVal(goodsAttrVal));
    }

    /**
     * 修改商品属性值
     */
    @PreAuthorize("@ss.hasPermi('goods:attr:edit')")
    @Log(title = "商品属性值", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SprdGoodsAttrVal goodsAttrVal)
    {
        if (!sprdGoodsAttrValService.checkGoodsAttrValKeyUnique(goodsAttrVal))
        {
            return error("修改属性值失败，属性键值" + goodsAttrVal.getValueKey() + "已存在");
        }
        if (!sprdGoodsAttrValService.checkGoodsAttrValLabelUnique(goodsAttrVal))
        {
            return error("修改属性值失败，属性值" + goodsAttrVal.getValueLabel() + "已存在");
        }
        goodsAttrVal.setUpdateBy(getUsername());
        return toAjax(sprdGoodsAttrValService.updateSprdGoodsAttrVal(goodsAttrVal));
    }

    /**
     * 删除商品属性值
     */
    @PreAuthorize("@ss.hasPermi('goods:attr:remove')")
    @Log(title = "商品属性值", businessType = BusinessType.DELETE)
    @DeleteMapping("/{valueIds}")
    public AjaxResult remove(@PathVariable Long[] valueIds)
    {
        return toAjax(sprdGoodsAttrValService.deleteSprdGoodsAttrValByValueIds(valueIds));
    }

    @PreAuthorize("@ss.hasPermi('goods:attr:edit')")
    @GetMapping("/findMaxOrderNum/{attrId}")
    public AjaxResult findMaxOrderNum(@PathVariable Long attrId) {
        return success(sprdGoodsAttrValService.findMaxOrderNum(attrId));
    }
}
